package uinv.com.uitl;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import net.sf.json.JSONObject;

public class readFile
{
  public static String readJsFile(String filePath)
  {
    String i = null;
    try {
      String encoding = "UTF-8";
      File file = new File(filePath);
      if ((file.isFile()) && (file.exists())) {
        InputStreamReader read = new InputStreamReader(
          new FileInputStream(file), encoding);
        BufferedReader bufferedReader = new BufferedReader(read);
        String lineTxt = null;
        while ((lineTxt = bufferedReader.readLine()) != null) {
          i = lineTxt;
        }
        read.close();
      } else {
        System.out.println("找不到指定的文件");
      }
    } catch (Exception e) {
      System.out.println("读取文件内容出错");
      e.printStackTrace();
    }
    return i;
  }

  public static JSONObject readTxtFileIntoStringArrList(String filePath)
  {
	JSONObject json = null;
    try
    {
      String encoding = "UTF-8";
      File file = new File(filePath);
      if ((file.isFile()) && (file.exists()))
      {
    	InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
    	BufferedReader reader=new BufferedReader(read);
    	StringBuilder sb=new StringBuilder();
    	String line=null;
    	while((line=reader.readLine())!=null){
    	//把换行符去掉
    	  sb.append(line);
    	}
    	reader.close();
    	//第二步把这个sb里面的真正的json内容拿到也就是从第一个"{"到最后一个"}"包括这两个{};
    	//这个是我们要的
    	String jsonString=sb.substring(sb.indexOf("{"),sb.lastIndexOf("}")+1);
    	//接下来给他转换成json-lib里的JSONObject对象
    	json=JSONObject.fromObject(jsonString);
    	System.out.println("-------------------------");
    	System.out.println("File read successfully !");
    	System.out.println("-------------------------");
      }
      else
      {
        //System.out.println("找不到指定的文件");
        System.out.println("------------------------------------");
    	System.out.println("Unable to find the specified file !");
    	System.out.println("------------------------------------");
      }
    }
    catch (Exception e)
    {
      //System.out.println("读取文件内容出错");
	    System.out.println("-------------------------");
	    System.out.println("Error reading file content !");
	    System.out.println("-------------------------");
	    //e.printStackTrace();
    }

    return json;
  }
}
